package com.fjwt.gz.task.autonomousActivation;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fjwt.gz.coupon.gtpay.GtCreateCouponService;
import com.fjwt.gz.db.dto.AwardCouponDTO;
import com.fjwt.gz.db.entity.ActivityEntity;
import com.fjwt.gz.db.entity.AwardCouponEntity;
import com.fjwt.gz.db.entity.AwardSettingEntity;
import com.fjwt.gz.service.impl.ActivityService;
import com.fjwt.gz.service.impl.AwardCouponService;
import com.fjwt.gz.service.impl.AwardSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ACTE026 进行商家卡券自主激活
 */
@Slf4j
@Component
public class AutonomousActivationTask {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private AwardCouponService awardCouponService;
    @Autowired
    private AwardSettingService awardSettingService;

//    @Scheduled(cron = "${cron.AutonomousActivationTask}")//每3秒执行一次
    @Transactional
    public void start() throws Exception {
        log.info("开始进入：商家卡券激活任务:{}", DateUtil.now());
        GtCreateCouponService gtCreateCouponService = new GtCreateCouponService();
        LambdaQueryWrapper<ActivityEntity> activityWrapper = new LambdaQueryWrapper<>();
        activityWrapper.eq(ActivityEntity::getActivityTemplateNo, "ACTE026");
        activityWrapper.eq(ActivityEntity::getState, 4);
        List<ActivityEntity> activityList = activityService.list(activityWrapper);

        for (ActivityEntity activityEntity : activityList) {
            List<AwardCouponDTO> list = awardCouponService.selectjh(activityEntity.getActivityId());
            log.info("符合模板编号为 ACTE026 的卡券活动信息表数据：{}",list);
            for (AwardCouponDTO awardCoupon : list) {
                String payConfig = awardCoupon.getPayConfig();
                JSONObject jsonObject = JSONUtil.parseObj(payConfig);
                if (ObjectUtils.isEmpty(jsonObject) || JSONUtil.isNull(jsonObject.get("stock_id"))) {
                    continue;
                }
                if (JSONUtil.isNull(jsonObject.get("stock_id"))){
                    awardSettingService.lambdaUpdate()
                            .set(AwardSettingEntity::getActivateExplain, "数据有误")
                            .eq(AwardSettingEntity::getAwardId, awardCoupon.getCouponId())
                            .update();
                    continue;
                }
                String b = gtCreateCouponService.activationResponse((String) jsonObject.get("stock_id"));
                if (b.equals("success")) {
                    awardSettingService.lambdaUpdate()
                            .set(AwardSettingEntity::getActivateExplain, "激活成功")
                            .eq(AwardSettingEntity::getAwardId, awardCoupon.getCouponId())
                            .update();
                }else {
                    awardSettingService.lambdaUpdate()
                            .set(AwardSettingEntity::getActivateExplain, b)
                            .eq(AwardSettingEntity::getAwardId, awardCoupon.getCouponId())
                            .update();
                }
            }
        }
        log.info("任务结束：商家卡券激活任务:{}", DateUtil.now());
    }
}
